package com.financial.service.heemoney.service.impl;

import com.financial.service.heemoney.constants.FinancialProregisterHeemoneylogEnumConstants;
import com.financial.service.heemoney.constants.FinancialRegisterHeemoneylogEnumConstants;
import com.financial.service.heemoney.convertor.HeemoneyProviderSubmitConvert;
import com.financial.service.heemoney.dao.FinancialProsubmitHeemoneyDao;
import com.financial.service.heemoney.dao.FinancialProsubmitHeemoneylogDao;
import com.financial.service.heemoney.dao.FinancialSubmitHeemoneylogDao;
import com.financial.service.heemoney.domain.FinancialProsubmitHeemoneyDO;
import com.financial.service.heemoney.domain.FinancialProsubmitHeemoneylogDO;
import com.financial.service.heemoney.domain.FinancialSubmitHeemoneylogDO;
import com.financial.service.heemoney.dto.HeemoneyConfigDTO;
import com.financial.service.heemoney.dto.businsess.HeemoneyProviderSubmitBusinessDTO;
import com.financial.service.heemoney.dto.commmon.HeemoneyProviderSubmitCommonDTO;
import com.financial.service.heemoney.dto.inparameter.HeemoneyProviderSubmitInParamDTO;
import com.financial.service.heemoney.dto.param.HeemoneyProviderSubmitParamDTO;
import com.financial.service.heemoney.service.FinancialProsubmitHeemoneyService;
import com.financial.service.heemoney.utils.JsonUtils;
import com.financial.service.heemoney.utils.OkHttpUtils;
import com.financial.service.heemoney.utils.ResultUtils;
import com.financial.service.heemoney.utils.apiresult.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.ws.rs.HEAD;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class FinancialProsubmitHeemoneyServiceImpl implements FinancialProsubmitHeemoneyService {
    @Autowired
    private FinancialSubmitHeemoneylogDao financialSubmitHeemoneylogDao;
    @Autowired
    private FinancialProsubmitHeemoneyDao financialProsubmitHeemoneyDao;
    @Autowired
    private FinancialProsubmitHeemoneylogDao financialProsubmitHeemoneylogDao;
    @Autowired
    private OkHttpUtils okHttpUtils;
    public static final Logger log = LoggerFactory.getLogger(FinancialPayHeemoneyServiceImpl.class);

    @Override
    public ApiResult providerSubmit(HeemoneyProviderSubmitInParamDTO heemoneyProviderSubmitInParamDTO) {
        log.info("调用金融中心汇收银服务层商户统一进件接口前参数打印：{}", JsonUtils.beanToJson(heemoneyProviderSubmitInParamDTO));
        //请求参数日志保存
        this.saveProviderSubmit(heemoneyProviderSubmitInParamDTO);
        //数据转实体校验参数非空
        HeemoneyProviderSubmitCommonDTO providerSubmitCommonDTO = HeemoneyProviderSubmitConvert.MAPPER.configDTOConvertCommonDTO(new HeemoneyConfigDTO(), heemoneyProviderSubmitInParamDTO);
        HeemoneyProviderSubmitBusinessDTO providerSubmitBusinessDTO = HeemoneyProviderSubmitConvert.MAPPER.configDTOConvertBusinessDTO(new HeemoneyConfigDTO(), heemoneyProviderSubmitInParamDTO);
        HeemoneyProviderSubmitParamDTO heemoneyProviderSubmitParamDTO = new HeemoneyProviderSubmitParamDTO(providerSubmitCommonDTO, providerSubmitBusinessDTO);
        log.info("http请求汇收银服务层商户统一进件接口入参：{}", heemoneyProviderSubmitParamDTO.getJsonParam());
        try {
            String resultStr = okHttpUtils.postJsonParams(new HeemoneyConfigDTO().getHeemoneyProviderSubmit().getRequest_url(), heemoneyProviderSubmitParamDTO.getJsonParam());
            log.info("http请求汇收银服务层商户统一进件接口回执:{}", resultStr);
            //商户进件业务数据保存
            this.saveSubmint(resultStr, heemoneyProviderSubmitInParamDTO);
            //返回调用信息
            return ResultUtils.resultStr(resultStr);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("http请求汇收银服务层商户统一进件接口失败：{}", e.getMessage());
        }
        return ApiResult.failure();
    }

    private void saveProviderSubmit(HeemoneyProviderSubmitInParamDTO heemoneyProviderSubmitInParamDTO) {
        try {
            FinancialSubmitHeemoneylogDO submitHeemoneylogDO = new FinancialSubmitHeemoneylogDO();
            BeanUtils.copyProperties(heemoneyProviderSubmitInParamDTO, submitHeemoneylogDO);
            submitHeemoneylogDO.setGmtModified(new Date());
            submitHeemoneylogDO.setGmtCreate(new Date());
            submitHeemoneylogDO.setIsDelete(FinancialProregisterHeemoneylogEnumConstants.ENUM_ISDELETE_UNDEL);
            financialSubmitHeemoneylogDao.save(submitHeemoneylogDO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("对接平台商户号：{}进件提交记录异常！", heemoneyProviderSubmitInParamDTO.getStore_no());
        }

    }

    private void saveSubmint(String resultStr, HeemoneyProviderSubmitInParamDTO heemoneyProviderSubmitInParamDTO) {
        try {
            Map<String, Object> strMap = JsonUtils.jsonToMap(resultStr);
            Object temp = "";
            temp = strMap.get("sub_mch_id");
            String sub_mch_id = temp != null ? temp.toString() : null;
            String result_code = strMap.get("result_code").toString();
            FinancialProsubmitHeemoneyDO prosubmitHeemoneyDO = new FinancialProsubmitHeemoneyDO();

            if (!result_code.equals("SUCCESS")) {
                prosubmitHeemoneyDO.setState(FinancialRegisterHeemoneylogEnumConstants.ENUM_STATE_FAIL);
            } else {
                prosubmitHeemoneyDO.setState(FinancialRegisterHeemoneylogEnumConstants.ENUM_STATE_SUC);
            }
            prosubmitHeemoneyDO.setMchUid(heemoneyProviderSubmitInParamDTO.getMch_uid());
            prosubmitHeemoneyDO.setMerchFullName(heemoneyProviderSubmitInParamDTO.getMerch_full_name());
            prosubmitHeemoneyDO.setOfficalType(heemoneyProviderSubmitInParamDTO.getOffical_type());
            prosubmitHeemoneyDO.setContactName(heemoneyProviderSubmitInParamDTO.getContact_name());
            prosubmitHeemoneyDO.setContactMobile(heemoneyProviderSubmitInParamDTO.getContact_mobile());
            prosubmitHeemoneyDO.setSubMchUid(sub_mch_id);
            prosubmitHeemoneyDO.setIsDelete(FinancialRegisterHeemoneylogEnumConstants.ENUM_ISDELETE_UNDEL);
            prosubmitHeemoneyDO.setGmtCreate(new Date());
            prosubmitHeemoneyDO.setGmtModified(new Date());
            financialProsubmitHeemoneyDao.save(prosubmitHeemoneyDO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("第三方商户号：{}商户进件提交业务数据保存失败！", heemoneyProviderSubmitInParamDTO.getMch_uid());
        }

    }


    private void updateProSubmitInfo(String resultStr, FinancialProsubmitHeemoneylogDO prosubmitHeemoneylogDO) {
        Map<String, Object> strMap = JsonUtils.jsonToMap(resultStr);
        Object temp = "";
        temp = strMap.get("sub_mch_id");
        String sub_mch_id = temp != null ? temp.toString() : null;
        String result_code = strMap.get("result_code").toString();
        try {
            prosubmitHeemoneylogDO.setGmtModified(new Date());
            prosubmitHeemoneylogDO.setIsDelete(FinancialRegisterHeemoneylogEnumConstants.ENUM_ISDELETE_UNDEL);
            prosubmitHeemoneylogDO.setSubMchUid(sub_mch_id.substring(sub_mch_id.indexOf("：") + 1).trim());
            if (!result_code.equals("SUCCESS")) {
                prosubmitHeemoneylogDO.setState(FinancialRegisterHeemoneylogEnumConstants.ENUM_STATE_FAIL);
            } else {
                prosubmitHeemoneylogDO.setState(FinancialRegisterHeemoneylogEnumConstants.ENUM_STATE_SUC);
            }
            prosubmitHeemoneylogDO.setReturnMessage(resultStr);
            financialProsubmitHeemoneylogDao.update(prosubmitHeemoneylogDO);
        } catch (Exception e) {
            log.error("修改汇收银商户统一进件信息失败！:{}", strMap.get("error_msg"));
            e.printStackTrace();
        }
    }


    @Override
    public FinancialProsubmitHeemoneyDO get(Integer id) {
        return financialProsubmitHeemoneyDao.get(id);
    }

    @Override
    public List<FinancialProsubmitHeemoneyDO> list(Map<String, Object> map) {
        return financialProsubmitHeemoneyDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return financialProsubmitHeemoneyDao.count(map);
    }

    @Override
    public int save(FinancialProsubmitHeemoneyDO financialProsubmitHeemoney) {
        return financialProsubmitHeemoneyDao.save(financialProsubmitHeemoney);
    }

    @Override
    public int update(FinancialProsubmitHeemoneyDO financialProsubmitHeemoney) {
        return financialProsubmitHeemoneyDao.update(financialProsubmitHeemoney);
    }

    @Override
    public int remove(Integer id) {
        return financialProsubmitHeemoneyDao.remove(id);
    }

    @Override
    public int batchRemove(Integer[] ids) {
        return financialProsubmitHeemoneyDao.batchRemove(ids);
    }


}
